<?php

namespace app\frontend;

use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\DiInterface;
use Phalcon\Mvc\Dispatcher;
use Phalcon\Mvc\ModuleDefinitionInterface;
use Phalcon\Mvc\View\Engine\Php as PhpEngine;
use Phalcon\Mvc\Url as UrlResolver;
use Phalcon\Mvc\View\Engine\Volt as VoltEngine;
use Phalcon\Assets\Manager as AssetManager;

class Module implements ModuleDefinitionInterface
{
    /**
     * 注册
     * @param DiInterface|null $di
     */
    public function registerAutoloaders(DiInterface $di = null)
    {
        $loader = new Loader();

        $loader->registerNamespaces(
            [
                "app\\frontend\\controllers" => __DIR__ . '/controllers/',
                "app\\frontend\\forms" => __DIR__ . '/forms/',
            ]
        );

        $loader->register();
    }

    /**
     * 注册自定义服务
     */
    public function registerServices(DiInterface $di)
    {
        /**
         * Database connection is created based in the parameters defined in the configuration file
         */
        $di->setShared('db', function () {
            $config = include APP_PATH . "/config/config.php";

            $class = 'Phalcon\Db\Adapter\Pdo\\' . $config->database->adapter;
            $connection = new $class([
                'host' => $config->database->host,
                'username' => $config->database->username,
                'password' => $config->database->password,
                'dbname' => 'insmanage',
                'charset' => $config->database->charset
            ]);

            return $connection;
        });
        $di->set('assets',function(){
            $manager = new AssetManager();
            $jsCollection = $manager->getJs()
                ->setPrefix('pc/')
                ->addJs('js/jquery-1.12.0.min.js')
                ->addJs('js/bootstrap.min.js');


            $cssCollection = $manager->getCss()
                ->setPrefix('pc/')
                ->addCss('css/bootstrap.min.css?v=3.3.5')
                ->addCss('css/common.css')
            ;

            $manager->set('js',$jsCollection);
            $manager->set('css',$cssCollection);

            return $manager;
        });
        // Registering the view component
        $di->set(
            "view",
            function () use ($di) {

                $view = new View();
                $view->setDI($di);
                $view->setViewsDir(__DIR__ . '/views/');

                $view->registerEngines([
                    '.volt' => function ($view) use ($di) {
                        $config = include APP_PATH . "/config/config.php";

                        $volt = new VoltEngine($view, $di);

                        $volt->setOptions([
                            'compiledPath' => $config->application->cacheDir . '/frontend/',
                            'compiledSeparator' => '_'
                        ]);

                        return $volt;
                    }
                ]);

                return $view;
            }
        );
    }
}